package sng.modules.view
{
	import de.polygonal.ds.Array2;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import sng.data.info.BuildingInfo;
	import sng.modules.model.CityProxy;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.HudVO;
	import sng.modules.system.CitySystem;
	import sng.modules.system.NameSpace;
	import sng.modules.view.components.city.logic.City;
	import sng.modules.view.components.city.logic.unit.DragManager;
	import sng.modules.view.components.city.panel.BuildingNamePanel;
	import sng.modules.view.components.city.state.CityDeleteState;
	import sng.modules.view.components.city.state.CityFriendState;
	import sng.modules.view.components.city.state.CityMoveState;
	import sng.modules.view.components.city.state.CityNewState;
	import sng.modules.view.components.city.state.CityNormalState;
	import sng.modules.view.components.city.state.CitySeedState;
	import sng.modules.view.components.city.state.CityTurnState;
	
	public class CityMediator extends Mediator implements IMediator
	{
		
		private var main:Main;
		private var cityProxy:CityProxy;
		private var city:City;
		private var buildingArray:Array2;
		private var buildingNamePanel:BuildingNamePanel;
		private var buildingVO:BuildingVO;
		
		public function CityMediator(app:Object)
		{
			super(CitySystem.CityMediatorName, app);
			
			cityProxy = CityProxy(facade.retrieveProxy(CitySystem.CityProxyName));
			main=app as Main;
			//1.用户建筑列表数组
			buildingArray=new Array2(City.GRID_WIDTH+City.arrayOffsetX*2, City.GRID_LENGTH+City.arrayOffsetY*2);
			//2.从后台导入用户数据至此数组
			
			
			city = new City(City.GRID_WIDTH, City.GRID_LENGTH, buildingArray, this);
			main.cityMain.addChild(city);
			
		}
		
		/**
		 * 
		 * @监听列表
		 * 
		 */
		override public function listNotificationInterests():Array 
		{
			
			return [ 
				CitySystem.CityStart,
				CitySystem.CityZoomNotice,
				NameSpace.HudBuildRequest,
				NameSpace.HudBuildMove,
				NameSpace.HudBuildDelete,
				NameSpace.HudBuildTurn,
				NameSpace.HudBuildCancel,
				NameSpace.HudCreateUserCity,
				NameSpace.HudCreateFriendCity,
				CitySystem.CityFullRequest,
				NameSpace.HudBuildSeed
			];
		}
		
		/**
		 * 
		 * @对监听结果进行处理
		 * 
		 */
		override public function handleNotification( note:INotification ):void 
		{
			switch (note.getName())
			{
				case CitySystem.CityZoomNotice:
				{
					//处理城市的缩放
					city.cityView.zoomFunction(Number(note.getBody()));
					break;
				}
				case NameSpace.HudCreateUserCity:
				{
					city.createCity(note.getBody() as HudVO);
					city.setCityState(new CityNormalState(city));
					//创建当前用户的城市
					break;
				}
				case NameSpace.HudCreateFriendCity:
				{
					city.createCity(note.getBody() as HudVO);
					city.setCityState(new CityFriendState(city));
					//创建当前用户的城市
					break;
				}
				case NameSpace.HudBuildRequest:
					var currVo:BuildingVO = note.getBody() as BuildingVO
					DragManager.getInstance().drag(currVo, city.cityView);
					city.setCityState(new CityNewState(city));//新建状态
//					city.currentState.Execute(currVo);
					
					//city.createBuilding(note.getBody() as BuildingVO);
					break;
				//删除建筑
				case NameSpace.HudBuildDelete:
					city.setCityState(new CityDeleteState(city));
					break;
				//移动建筑
				case NameSpace.HudBuildMove:
					city.setCityState(new CityMoveState(city));
					break;
				//删除建筑
				case NameSpace.HudBuildTurn:
					city.setCityState(new CityTurnState(city));
					break;
				//取消当前状态
				case NameSpace.HudBuildCancel:
				{
					city.cancelCreateBuilding();
					city.setCityState(new CityNormalState(city));
					break;
				}
				//播种状态
				case NameSpace.HudBuildSeed:
				{
					BuildingInfo.seedid= note.getBody() as uint;//种子ID
					city.cancelCreateBuilding();
					city.setCityState(new CitySeedState(city));
					break;
				}
					//全屏状态
				case CitySystem.CityFullRequest:
				{
					//已经不需要
					//city.cityView.setSize(city.stage.width,city.stage.height);
					
					//更改view的大小 eason 20110720
					city.cityView.setSize(note.getBody()[0],note.getBody()[1]);
					//检查地图是否出界 eason 20110718
					city.cityView.checkRange();
					break;
				}
			}
		}
		
		/**
		 * 首次建商业建筑命名
		 * 
		 */		
		public function nameBuilding(vo:BuildingVO, userName:String):void
		{
			buildingNamePanel = new BuildingNamePanel();
			buildingNamePanel.addEventListener(CitySystem.BusinessBuidingName, onBuildingName);
			PopUpManager.addPopUp(buildingNamePanel, main);
			PopUpManager.centerPopUp(buildingNamePanel);
			
			buildingVO = vo;
			buildingVO.buildingname = userName+"的"+buildingVO.buildingname;
			buildingNamePanel.nameTextinput.text = buildingVO.buildingname;
			buildingNamePanel.nameTextinput.selectAll();
		}
		
		protected function onBuildingName(event:Event):void
		{
			buildingVO.buildingname = buildingNamePanel.nameTextinput.text;
			PopUpManager.removePopUp(buildingNamePanel);
			buildingNamePanel = null;
			buildingVO = null;
		}
	}
}